/*
 * Copyright 2011 Kim Lindhardt Madsen
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package dk.lindhardt.gwt.geie.shared;

import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

import java.io.Serializable;

/**
 * User: AnAmuser
 * Date: 07-08-11
 * <p/>
 * Cell representing an error in some cell. This cell wrappes the original cell where the error occurred.
 * The value of the error cell is the input that caused the error.
 */
@SuppressWarnings({"GWTStyleCheck"})
public class ErrorCell extends AbstractCell implements CellWrapper {

   private String message;
   private Cell wrappedCell;

   public ErrorCell(Serializable cause, String message, Cell wrappedCell) {
      super(cause, wrappedCell.getPosition(), wrappedCell.getCellFormatKey());
      this.message = message;
      this.wrappedCell = wrappedCell;
   }

   public String getMessage() {
      return message;
   }

   public Cell getWrappedCell() {
      return wrappedCell;
   }

   public boolean unWrap(TableLayout layout) {
      return layout.replaceCell(this, wrappedCell);
   }

   public Widget toWidget(CellFormat format) {
      Label label = new Label("Error");
      label.addStyleName("geie-error-cell");
      label.setTitle(getMessage());
      return wrapIfLink(label);
   }

   public Cell copy() {
      return new ErrorCell(getValue(), getMessage(), wrappedCell.copy());
   }
}
